import { Injectable } from '@nestjs/common';
import { CategoryDto } from './dto/categoryDto.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { Category } from './entities/category.entity';
import { Repository } from 'typeorm';
import { Tag } from './entities/tag.entity';
import { BlogDto } from './dto/blogDto.dto';
import { Blog } from './entities/blog.entity';


@Injectable()
export class BlogService {


  @InjectRepository(Category)
  private category:Repository<Category>
  @InjectRepository(Tag)
  private tag:Repository<Tag>
  @InjectRepository(Blog)
  private blog:Repository<Blog>

  // 创建博客分类
  async createCategory(categoryDtoInfo: CategoryDto) {
    return await this.category.save(categoryDtoInfo)
  }

  // 查询博客分类列表
  async getCategoryList() {
    return await this.category.find()
  }

  //修改博客分类
  async updateCategory(categoryDtoInfo: CategoryDto) {
    return await this.category.save(categoryDtoInfo)
  }

  //删除博客分类
  async deleteCategory(id: number) {
    return await this.category.delete(id)
  }

   // 创建博客标签
   async createTag(tagDto:any) {
    return await this.tag.save(tagDto)
  }

  // 查询博客标签列表
  async getTagList() {
    return await this.tag.find()
  }

  //修改博客标签
  async updateTag(obj) {
    return await this.tag.save(obj)
  }

  //删除博客标签
  async deleteTag(id: number) {
    return await this.tag.delete(id)
  }


   // 创建博客
   async createBlog(tagDto) {
    const category = await this.category.findOneBy({
      id:tagDto.category_id
    })
    console.log('分类的对象',category)
    if(!category){
      return '分类id错误'
    }
    tagDto.tags = [
      {
          "id": 2,
          "create_at": "2024-01-17T06:23:41.598Z",
          "update_at": "2024-01-17T06:23:41.598Z",
          "name": "前端"
      },
      {
          "id": 3,
          "create_at": "2024-01-17T06:47:51.809Z",
          "update_at": "2024-01-17T06:47:51.809Z",
          "name": "后端"
      }
  ]
    tagDto.category = category
    
    return await this.blog.save(tagDto)
  }

  // 查询博客列表
  async getBlogList() {
    return await this.tag.find()
  }

  //修改博客
  async updateBlog(obj) {
    return await this.tag.save(obj)
  }

  //删除博客
  async deleteBlog(id: number) {
    return await this.tag.delete(id)
  }

}
